package com.dm.cloud.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dm.cloud.basepo.service.SnowflakeGenId;
import com.dm.cloud.tkmybatis.service.BaseMapper;
import com.dm.cloud.auth.mapper.AuthOrgMapper;
import com.dm.cloud.auth.service.AuthOrgService;
import com.dm.cloud.auth.common.po.AuthOrg;
import com.dm.cloud.core.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
   * @author hcx
   * @type AuthOrgServiceImpl.java
   * @desc AuthOrg serviceImpl
   */
@Service

@Transactional

public class AuthOrgServiceImpl implements AuthOrgService {


    @Autowired
    private AuthOrgMapper authOrgMapper;


    @Override
    public BaseMapper getMapper() {
        return authOrgMapper;
    }
    public void processBeforeOperation(AuthOrg entity, BaseOperationEnum operationEnum) throws CustomException {
        if(operationEnum.equals(BaseOperationEnum.INSERT)){
            String id= SnowflakeGenId.genId();
            entity.setId(id);
            if(StrUtil.isNotEmpty(entity.getPId())){
                AuthOrg parentEntity = authOrgMapper.selectByPrimaryKey(entity.getPId());
                String idPath=parentEntity.getIdPath()+"/"+id;
                entity.setIdPath(idPath);
            }else{
                entity.setPId("");
                entity.setIdPath(id);
            }
        }

    }

    public void processAfterOperation(AuthOrg entity, BaseOperationEnum operationEnum) throws CustomException {
    }


}
